﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Restaurants
{
    public class Restaurant:IRestaurant

    {
        public RestaurantData GetRestaurantData(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            RestaurantData data = new RestaurantData();

            return data;
 
          //  data.branches = context.RestaurantBranches.Where(p => p.RestaurantID == restaurantId).ToList();
         
        }

        public int GetRestaurantID(string restaurantName, ref Model.NutriLeafDBEntities context)
        {
            return context.Restaurants.Where(p =>  p.RestaurantName.CompareTo(restaurantName)==0).First().RestaurantID;
        }

        public int GetRestaurantID(RestaurantData restaurant, ref Model.NutriLeafDBEntities context)
        {
            return context.Restaurants.Where(p => p.RestaurantName.CompareTo(restaurant.restaurant.RestaurantName) == 0).First().RestaurantID;
        }

        public List<RestaurantBranchData> GetRestaurantBranchData(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
         //doubt
            return new List<Restaurants.RestaurantBranchData>();
        }

        public List<Model.RestaurantBranch> GetRestaurantBranch(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            return context.RestaurantBranches.Where(p => p.RestaurantID == restaurantId).ToList();
        }

        public List<Model.RestaurantFood> GetRestaurantFoods(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            return context.RestaurantFoods.Where(p => p.RestaurantID == restaurantId).ToList();
        }

        public List<RestaurantFoodData> GetRestaurantFoodData(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            return new List<RestaurantFoodData>();
       
        }

        public Model.RestaurantFood GetRestaurantFood(int restaurantFoodId, ref Model.NutriLeafDBEntities context)
        {
            return context.RestaurantFoods.Where(p => p.RestaurantFoodID == restaurantFoodId).First();
        }

        public Model.Restaurant GetRestaurant(int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            return context.Restaurants.Where(p => p.RestaurantID == restaurantId).First();
        }

        public void InsertRestaurant(RestaurantData restaurantData, ref Model.NutriLeafDBEntities context)
        {
         //unknonwn
        }

        public void InsertRestaurant(List<RestaurantData> restaurantData, ref Model.NutriLeafDBEntities context)
        {
           //unknown
        }

        public void InsertRestaurantFood(List<Model.RestaurantFood> restaurantFood, ref Model.NutriLeafDBEntities context)
        {
            foreach (Model.RestaurantFood food in restaurantFood) {
                context.RestaurantFoods.AddObject(food);
            }
            context.SaveChanges();
        }

        public void InsertRestaurantFoodData(List<RestaurantFoodData> foodData, int restaurantId, ref Model.NutriLeafDBEntities context)
        {
           //unknown
        }

        public void InsertRestaurantBranch(List<Model.RestaurantBranch> branch, ref Model.NutriLeafDBEntities context)
        {
            foreach (Model.RestaurantBranch r in branch) {
                context.RestaurantBranches.AddObject(r);
            }
            context.SaveChanges();
        }

        public void InsertRestaurantBranchData(List<RestaurantBranchData> branch, int restaurantId, ref Model.NutriLeafDBEntities context)
        {
          //unknown
        }

        public void InsertRestaurantFood(Model.RestaurantFood restaurantFood, ref Model.NutriLeafDBEntities context)
        {
            context.RestaurantFoods.AddObject(restaurantFood);
            context.SaveChanges();
        }

        public void InsertRestaurantFoodData(RestaurantFoodData foodData, int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void InsertRestaurantBranch(Model.RestaurantBranch branch, ref Model.NutriLeafDBEntities context)
        {
            context.RestaurantBranches.AddObject(branch);
            context.SaveChanges();
        }

        public void InsertRestaurantBranchData(RestaurantBranchData branch, int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void RemoveRestaurant(int restauirantId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.RestaurantBranches.Where(p => p.RestaurantID == restauirantId).First());
            context.SaveChanges();
        }

        public void RemoveRestaurantBranch(int restaurantBranchId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.RestaurantBranches.Where(p => p.RestaurantBranchID == restaurantBranchId).First());
            context.SaveChanges();
        }

        public void RemoveRestaurantFood(int restaurantFoodId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.RestaurantFoods.Where(p => p.RestaurantFoodID == restaurantFoodId).First());
            context.SaveChanges();

        }
        public void UpdateRestaurantBranch(Model.RestaurantBranch branch, ref Model.NutriLeafDBEntities context)
        {
        //requires update
        }

        public void UpdateRestaurant(Model.Restaurant restaurant, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void UpdateRestaurantFood(Model.RestaurantFood food, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public List<General.StringIdPair> GetNearestRestaurantsSIPair(Model.Location location, int count, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public List<Model.Restaurant> GetNearestRestaurants(Model.Location location, int count, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public List<RestaurantData> GetNearestRestaurantData(Model.Location location, int count, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public List<string> GetRestaurantNames(string startsWith, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public RestaurantData GetRestaurantData(string restaurantName, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }

        public void VisitRestaurant(Model.UserRestaurant userRestaurant, ref Model.NutriLeafDBEntities context)
        {
            context.UserRestaurants.AddObject(userRestaurant);
            context.SaveChanges();
        }

        public void VisitRestaurant(int userId, int restaurantId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserRestaurant r = new Model.UserRestaurant();
            r.RestaurantID = restaurantId;
            r.UserID = userId;
            context.UserRestaurants.AddObject(r);
            context.SaveChanges();
        }

        public void EatFoodInRestaurant(Model.UserRestaurantFood userRestaurantFood, ref Model.NutriLeafDBEntities context)
        {
            context.UserRestaurantFoods.AddObject(userRestaurantFood);
            context.SaveChanges();
        }

        public void EatFoodInRestaurant(int userRestaurantId, int foodId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserRestaurantFood food = new Model.UserRestaurantFood();
            food.FoodID = foodId;
            food.UserRestaurantID = userRestaurantId;
            context.UserRestaurantFoods.AddObject(food);
            context.SaveChanges();
        }

        public List<Model.Food> GetAllFoodsUserAteInRestaurantVisit(int userRestaurantId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Food> foods = new List<Model.Food>();
            List<Model.UserRestaurantFood> food = context.UserRestaurantFoods.Where(p => p.UserRestaurantID == userRestaurantId).ToList();
            foreach (Model.UserRestaurantFood f in food) {
                foods.Add(f.Food);
                
            }
            return foods;
  
        }

        public List<Model.UserRestaurantFood> GetAllUserRestaurantFoods(int userRestaurantId, ref Model.NutriLeafDBEntities context)
        {
            return context.UserRestaurantFoods.Where(p => p.UserRestaurantID == userRestaurantId).ToList();
        }

        public void RemoveUserRestaurantFood(int userRestaurantFoodId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.UserRestaurantFoods.Where(p => p.UserRestaurantFoodsID == userRestaurantFoodId).First());
            context.SaveChanges();
        }

        public void RemoveUserRestaurant(int userRestaurantId, ref Model.NutriLeafDBEntities context)
        {
           List<Model.UserRestaurantFood> s= context.UserRestaurantFoods.Where(p => p.UserRestaurantID == userRestaurantId).ToList();
           foreach (Model.UserRestaurantFood f in s) {
               context.DeleteObject(f);
               context.SaveChanges();
           }
           context.DeleteObject(context.UserRestaurants.Where(p => p.UserRestaurantID == userRestaurantId).First());
           context.SaveChanges();
        }

        public List<Model.RestaurantBranch> SuggestRestaurant(int userId, Model.Location location, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
           return context.RestaurantBranches.Where(p => p.UserID == userId && p.Location == location && p.DateTimeStamp == datetime).ToList();
        }

        public List<RestaurantData> SuggestRestaurantData(int userId, Model.Location location, DateTime datetime, ref Model.NutriLeafDBEntities context)
        {
            throw new NotImplementedException();
        }
    }
}